package com.company.笔试.设计模式.组合模式;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Composite
 * @Description TODO
 * @Author 11
 * @Date 2021/6/8 17:55
 * @Version 1.0
 */

//容器
public class Composite implements Couter {
    private List<Couter> subordinates = new ArrayList<>();


    public void add (Couter composite){
        this.subordinates.add(composite);
    }
    public void delete(Composite composite){
        subordinates.remove(composite);
    }
    public List<Couter> getSubordinates(){
        return subordinates;
    }


    public int count(){
        return subordinates.stream().mapToInt(Couter::count).sum();//交给Counter的叶子节点的count函数处理
    }

    public static void main(String[] args) {
        Composite china = new Composite();
        china.add(new City(100));
        china.add(new City(1000));
        china.add(new City(10000));

        Composite jiangsu = new Composite();
        jiangsu.add(new City(2));
        jiangsu.add(new City(3));
        jiangsu.add(new City(6));
        china.add(jiangsu);
        System.out.println(china.count());

    }

}


//计数接口
interface Couter{
    //计数
    int count();
}

//叶子节点
class City implements Couter{
    private int sum;

    public City(int sum) {
        this.sum = sum;
    }

    @Override
    public int count() {
        return sum;
    }
}